import java.util.*;

import java.io.*;

public class ColScanner {
	String collectionName;
	String collectionNamePath;
	
	ColScanner(String collectionNamePath){
		this.collectionNamePath = collectionNamePath;
		
	}
	
	int build(){
		//File pointer for the collection;
		File colFp = new File(collectionNamePath);
		
		// build the directory for index files
		File dir = new File(colFp.getPath()+"_Index");
		dir.mkdir();	//make new directory [collection].txt_Index
		
		int docId=0, paraId=0, prevDocId=0, prevParaId=0;
		long paraLoc, wordLoc, nowLoc=0,prevWordLoc;
		String word,sentence,line1;
		WordListManager wlm = new WordListManager();
		String docStr, paraStr;
		
		StopWord sw = new StopWord();
		//sw.copyStopWords();
		
		//opening the file for reading data
		try{
			
			RandomAccessFile br = new RandomAccessFile(colFp,"rw");
	
			while((line1 = br.readLine())!=null){
			
				nowLoc = br.getFilePointer();
				docStr = br.readLine();
				prevDocId = docId;
				docId = Integer.parseInt(docStr);
				if(prevDocId != docId & Settings.stats){
					Settings.no_documents++;
					System.out.println("Document no = "+docId);
				}
				
				nowLoc = br.getFilePointer();
				paraStr = br.readLine();
				nowLoc = br.getFilePointer();
				prevParaId = paraId;
				paraId = Integer.parseInt(paraStr);
				if(prevParaId!=paraId & Settings.stats){
					Settings.no_paragraphs++;
					System.out.printf("		Paragraph no = %1$-10d ",paraId);
					System.out.println("Paragraph location = "+nowLoc);
				}
				
				
				
				
				paraLoc=nowLoc;
				wordLoc = nowLoc;									//copy offset to wordLoc 
				sentence = br.readLine();
		
				nowLoc = br.getFilePointer();
				
				//breaking words using scanner class
				Scanner sent = new Scanner(sentence);
				
				
				while(sent.hasNext()){
					
					word = sent.next();
					prevWordLoc = wordLoc;
					
					wordLoc+=(word.length()+1);				//address of words within a sentence (added with nowLoc)
					
					word = wlm.cleanWord(word); //clean to remove punctuation marks and make it to lowercase
					
					if(!sw.isStopWord(word)){
						wlm.makeWordtile(docId, paraId, paraLoc, prevWordLoc, word); //new object WordTile to hold wordtile data
						//wlm.displayWordtile();
						wlm.writeWordtile(dir);	//writing the word to proper file on disk
					}
					
					
					
				}
				if(Settings.debug){
					System.out.println(""+docId+" "+paraId+" "+sentence);
					System.out.println(""+paraLoc +" "+ wordLoc);
				}
				
				
				//if(i==100)break;
				//i++;	
				
			}
			br.close(); //close file
			
			
		}catch(FileNotFoundException e){
			System.out.println("File not found");
			e.printStackTrace();
		}catch(IOException e){
			System.out.println("IO excception");
			e.printStackTrace();
		}catch(Exception e){
			e.printStackTrace();
		}
		
		
		return 0;
	}
	
	
}
